<!DOCTYPE html>
<?php
$frist=!count($_POST);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if($frist){
        //第一次访问渲染页面
        ?>
        <h1>添加用户</h1>
        <form action="adduser.php" method="post"  enctype="multipart/form-data">
            账户：<input type="text" name="username"/><br/>
            密码：<input type="password" name="upwd"/><br/>
            性别：<select name="sex">
                <option>男</option>
                <option>女</option>
            </select>
            <br/>
            年龄：
            <select name="age">
                <?php
                for($index=18;$index<=100;$index++){
                    echo "<option>$index</option>";
                }
                ?>
            </select>
            <br/>
            头像：<input type="file" name="myimg"/><br/>
            <input type="submit" value="确定"/>
            <input type="reset" value="取消"/>
        </form>
        <?php
        }else{
            //不是第一次访问的话 就处理数据
           $name=$_POST["username"]; 
           $upwd=$_POST["upwd"];
           $age=$_POST["age"];
           $sex=$_POST["sex"];
           $newImgPath="img/".time()."_".$_FILES["myimg"]["name"];
           move_uploaded_file($_FILES["myimg"]["tmp_name"], $newImgPath);
           $pic=$newImgPath;
           
           
        $hostname="127.0.0.1";
        $username="root";
        $password="123456";
        $database="myshop";
        $connect=mysqli_connect($hostname, $username, $password, $database);
        //设置连接的字符集
        mysqli_set_charset($connect, "utf-8");
        if($connect){
            $sqlstring="insert into userinfos(username,upasswd,pic,sex,age)"
                    . " values('${name}','${upwd}','${pic}','${sex}',${age})";
            $result=mysqli_query($connect, $sqlstring);
            if($result){
                echo  "<script>alert('添加成功！');"
                . "location='mysql001.php';</script>";
            }else{
                echo "添加失败";
            }
        }
        mysqli_close($connect);
        }
        ?>
        
        <?php
        // put your code here
        ?>
    </body>
</html>
